package com.epoint.mobileoa.ui.login;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.FrameParam;
import com.epoint.mobileoa.http.HttpHelp;
import com.epoint.mobileoa.tools.MDUtil;
import com.epoint.mobileoa.tools.PhoneHelp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


/**
 * @author liyc
 * @time 2012-4-18 上午10:18:54
 * @annotation 身份注册功能
 */
public class RegisterView extends Activity implements OnClickListener {
	private EditText imeiEditText,imei_password,imei_name;
	private Button regisButton;
	private TextView tvNet;
	private String deviceid;
	public FrameParam fp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registerview);
		tvNet = (TextView) findViewById(R.id.tvNet);
		tvNet.setOnClickListener(this);
		imeiEditText = (EditText) findViewById(R.id.imei_et);
		imeiEditText.setEnabled(false);
		regisButton = (Button) findViewById(R.id.regis_button);
		regisButton.setOnClickListener(this);
		imei_password = (EditText) findViewById(R.id.imei_password);
		imei_name = (EditText) findViewById(R.id.imei_name);
		deviceid = PhoneHelp.getDeviceId(this);
		imeiEditText.setText(deviceid);
		setTitle(getString(R.string.prjname)+"V"+getString(R.string.version));
		fp = (FrameParam) getApplication();
	}


	@Override
	public void onClick(View v) {
		if(v==tvNet){
			String uri = tvNet.getText().toString();
			PhoneHelp.invokeSystemBrowser(this, uri);
		}
		else if(v==regisButton){
			
			String pswjm = MDUtil.authPassword(imei_password.getText().toString().trim());
			String uname = imei_name.getText().toString().trim();

			if(imei_name.getText().toString().trim().length()==0||imei_password.getText().toString().trim().length()==0){
				Toast.makeText(this, "注册用户名和密码不能为空!", 1000).show();
				return;
			}
			String seamurl = fp.generateUrl("MobileOARegist",uname,pswjm,deviceid);
			try{
				String bs = HttpHelp.getHttpBack(seamurl);
				if(bs.equals("Regist_Failure")){
					Toast.makeText(this, "注册失败,身份验证错误!", 1000).show();
				}
				else if(bs.equals("Regist_Sending")){
					Toast.makeText(this, "注册信息发送成功,请等待审核！", 1000).show();
				}
				else if(bs.equals("Regist_Already_Exist")){
					Toast.makeText(this, "该身份已经注册过！", 1000).show();
				}
				else if(bs.equals(HttpHelp.CONNECTION_TIME_OUT)){
					Toast.makeText(this, "注册失败，请检查IP设置是否正确！", 1000).show();
				}
				else{
					Toast.makeText(this, "注册失败", 1000).show();
				}
				
			}catch(Exception e){
				Toast.makeText(this, "注册失败", 1000).show();
			}
		}
	}
}
